<?php


namespace app\admin\controller\v1;


use app\admin\model\ShareUrlModel;
use app\admin\Validate\shareUrl\AddShareUrlValidate;
use app\admin\Validate\shareUrl\GetShareUrlValidate;
use app\admin\Validate\shareUrl\UpdateShareUrlStatusValidate;
use app\admin\Validate\shareUrl\UpdateShareUrlValidate;
use app\BaseController;
use app\common\Token;
use app\common\validate\IdMustIntValidate;
use think\facade\Validate;

/**
 * 友情链接
 * Class ShareUrl
 * @package app\admin\controller\v1
 */
class ShareUrl extends BaseController
{
    /**
     * 添加友情链接
     * @return \think\response\Json
     */
    public function addShareUrl() {
        Token::checkToken($this->request);
        $param = (new AddShareUrlValidate())->validateParam();
        $model = new ShareUrlModel();
        $param['add_time'] = getDateTime();
        $result =  $model->save($param);
        if ($result) {
            return successJson(true, "添加成功");
        }
        return errorJson(60001, "添加失败");
    }

    /**
     * 更新友情链接
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateShareUrl() {
        Token::checkToken($this->request);
        $param = (new UpdateShareUrlValidate())->validateParam();
        $model = new ShareUrlModel();
        $param['update_time'] = getDateTime();
        $re = $model->find($param['id']);
        if(!$re){
            return errorJson(60004, "id对应的数据不存在");
        }
        $success = $re::update($param);
        if($success){
            return successJson(true, "更新成功");
        }
        return errorJson(60003, "更新失败");
    }

    /**
     * 获取友情链接
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function getShareUrlList() {
        Token::checkToken($this->request);
        $param = (new GetShareUrlValidate())->validateParam();
        $model = new ShareUrlModel();
        $lists = $model->toPaginate($param);
        return successJson($lists, "获取成功");
    }

    /**
     * 通过id更新对应的状态
     * @return \think\response\Json
     */
    public function updateShareUrlStatus() {
        Token::checkToken($this->request);
        $param = (new UpdateShareUrlStatusValidate())->validateParam();
        $success = ShareUrlModel::update($param);
        if($success){
            return successJson(true, "更新成功");
        }
        return errorJson(60003, "更新失败");
    }

    /**
     * 根据id删除后数据
     * @param int $id
     * @return \think\response\Json
     */
    public function deleteShareUrl(int $id){
        (new IdMustIntValidate())->validateParam();
        $success = ShareUrlModel::destroy($id);
        if($success){
            return successJson(true, "删除成功");
        }
        return errorJson(60002, "删除失败");
    }

    /**
     * 通过id获取友情链接
     * @param int $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getShareUrlById(int $id){
        Token::checkToken($this->request);
        (new IdMustIntValidate())->validateParam();
        $result = (new ShareUrlModel())->find($id);
        if($result){
            return successJson($result, "获取成功");
        }
        return errorJson(60004, "获取失败");
    }
}